﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing;

namespace GeekShop
{
    public class PictureManager
    {
        #region Fields
        private List<Picture> pictures = new List<Picture>();
        private Form mdiParent = null;
        private Picture selectedPicture = null;
        #endregion

        #region Properties
        internal List<Picture> Pictures
        {
            get { return pictures; }
            set { pictures = value; }
        }
        public Form MdiParent
        {
            get { return mdiParent; }
            set 
            { 
                this.mdiParent = value;
                this.mdiParent.MdiChildActivate += new EventHandler(MdiChildActivated);
            }
        }
        public Picture SelectedPicture
        {
            get { return selectedPicture; }
            set { selectedPicture = value; }
        }
        #endregion

        #region Virtual Properties
        public bool Empty
        { get { return (this.pictures.Count > 0); } }
        #endregion

        #region Constructors
        public PictureManager()
        { }
        public PictureManager(Form mdiParent)
        {
            this.MdiParent = mdiParent;
        }
        #endregion

        #region Event Methods
        private void MdiChildActivated(object sender, EventArgs e)
        {
            if (this.mdiParent.ActiveMdiChild != null)
            {
                this.selectedPicture = ((PictureForm)this.mdiParent.ActiveMdiChild).Picture;
            }
            else
            {
                this.selectedPicture = null;
            }
        }        
        #endregion

        #region Public Methods
        public void Add(Picture picture)
        {
            this.pictures.Add(picture);
        }
        public void Load(string fileName)
        {
            if (File.Exists(fileName))
            {
                Layer bgLayer = new Layer()
                {
                    Name = "Background",
                    Opacity = 100,
                    Visible = true,
                    Bitmap = (Bitmap)Bitmap.FromFile(fileName)
                };
                Picture picture = new Picture(bgLayer, this.mdiParent)
                {
                    Path = fileName
                };
                this.Add(picture);
                this.selectedPicture = picture;
                ((MainForm)this.mdiParent).MdiChildActivated(picture.Form, new EventArgs());
            }
        }
        #endregion
    }
}
